#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

FILE *OutFd = NULL, *InFd = NULL;
char FileNum = 0;
char File[20][60], *buf = NULL, *FileBuf = NULL;
#ifdef __linux__
const char rootdir[] = "/tmp/%s_tmp/";
const char clearcmd[] = "rm -rf";
const char startcmd[] = "/tmp/%s_tmp/%s";
#else
const char rootdir[] = "C:\\%s_tmp\\";
const char clearcmd[] = "rmdir /s /q";
const char startcmd[] = "init.bat C:/%s_tmp/%s";
#endif

char Confdir[256] = {0};

void clear (void)
{
	int i = 0;
	char param[64] = {0};

	if (0 == access(Confdir, F_OK))
	{
		snprintf(param, sizeof(param), "%s %s", clearcmd, Confdir);
		if (0 != system(param))
		{
			printf("Please Upgrade permissions\r\n");
		}
	}

	if (NULL != OutFd)
	{
		fclose(OutFd);
		OutFd = NULL;
	}

	if (NULL != InFd)
	{
		fclose(InFd);
		InFd = NULL;
	}

	if (NULL != FileBuf)
	{
		free(FileBuf);
		FileBuf = NULL;
	}

	if (NULL != buf)
	{
		free(buf);
		buf = NULL;
	}
}

/******************************************************************
Main Function
******************************************************************/
int main(int argc, char* argv[])
{
	int i = 0, j = 0, size = 0, rnum = 0, wsize,bufsize;
	char   param[1024] = {0};
    int    ret = 0;
	char   tmpDir[1024] = {0};
	char   tmpName[256] = {0};

	/* BIOS_VENDOR是编译时传递下来的，这个是为了speedup这种不允许带昆仑字样客户准备的 */
#ifdef __linux__
	setenv("BIOS_VENDOR", BIOS_VENDOR, 1);
	setenv("COPY_RIGHT", COPY_RIGHT, 1);
#else
	_putenv_s("BIOS_VENDOR", BIOS_VENDOR);
	_putenv_s("COPY_RIGHT", COPY_RIGHT);
#endif

	for (i = strlen(argv[0]) - 1; i > 0 ; i--)
	{
		if ('/' == argv[0][i])
		{
			i = i + 1;
			break;
		}
		else if( '\\' == argv[0][i] )
		{
			i = i + 1;
			break;
		}
	}

    snprintf(tmpName, sizeof(tmpName), "%s", &argv[0][i]);
	snprintf(Confdir, sizeof(Confdir), rootdir, tmpName);

	if (0 == access(Confdir, F_OK))
	{
		snprintf(param, sizeof(param), "%s %s", clearcmd, Confdir);
		if (0 != system(param))
		{
			printf("Please Upgrade permissions\r\n");
			return -1;
		}
	}

	snprintf(param, sizeof(param), "mkdir %s", Confdir);
	if (0 != system(param))
	{
		printf("Please Upgrade permissions\r\n");
		return -1;
	}

	InFd = fopen(argv[0], "rb");
	if (NULL == InFd)
	{
		printf("Open file err\r\n");
		return -1;
	}
	
	fseek(InFd, -1, 2);
	fread(&FileNum, 1, 1, InFd);

	/* 1:num 4:size 30:name */
	bufsize = (1 + 34 * FileNum);
	fseek(InFd, -1 * bufsize, 2);
	buf = (char *)calloc(1, bufsize);
	fread(buf, 1, bufsize, InFd);
	
	fseek(InFd, 0, 0);
	for (i = 0; i < FileNum; i++)
	{
			snprintf(File[i], sizeof(File[i]), "%s", &buf[30*i]);
		 	snprintf(tmpDir, sizeof(tmpDir), "%s%s", Confdir, File[i]);
		 	for (j = 0; j < 4; j++)
			{
				size += (((buf[30 * FileNum + i * 4 + j]) & 0x0FF) << (8 * j));
		 	}

			FileBuf = (char *)calloc(1, size);
			if (NULL == FileBuf)
			{
				printf("calloc Fail\r\n");
				ret = -1;
				goto Error;
			}

			rnum = fread(FileBuf, 1, size, InFd);
			if (rnum != size)
			{
				printf("read %d %d file %d\r\n", rnum, size, i);
				ret = -1;
				goto Error;
			}

			if (i == 0)
			{
				size = 0;
				free(FileBuf);
				FileBuf = NULL;
				continue;
			}
			
			OutFd = fopen(tmpDir, "wb+");
			wsize = fwrite(FileBuf, 1, size, OutFd);
			if (wsize != size)
			{
				printf("write %d %d file %d\r\n", wsize, size, i);
				ret = -1;
				goto Error;
			}

			fclose(OutFd);
			OutFd = NULL;
			size = 0;
#ifdef __linux__
			snprintf(param, sizeof(param), "chmod 777 %s", tmpDir);
			if (0 != system(param))
			{
				ret = -1;
				goto Error;
			}
#endif

			free(FileBuf);
			FileBuf = NULL;
	}

	fclose(InFd);
	InFd = NULL;
	free(buf);
	buf = NULL;
	
	snprintf(param, sizeof(param), startcmd, tmpName, File[1]);
	for (i = 1; i < argc; i++)
	{
		if(strlen(param ) == (sizeof(param) - 1)) 
		{
			printf("Params  exceeded \n");
			break;
		}
		snprintf(param + strlen(param), sizeof(param) - strlen(param), " \"%s\"", argv[i]);
	}
	
	if (0 != system(param))
	{
		ret = 1;
		goto Error;
	}

	Error:
	
		clear();

	return ret;
}


